package com.fg.cloud.agency.controller.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.fg.cloud.agency.controller.BaseAgencyController;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.ProblemStatus;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.manager.entry.system.ProblemInteractiveEntity;
import com.fg.cloud.manager.entry.system.SystemProblemEntity;
import com.fg.cloud.manager.service.agency.AgencyService;
import com.fg.cloud.manager.service.system.SystemProblemService;

@Controller
@RequestMapping("/systemPro")
public class AgencyProblemController extends BaseAgencyController {

	@Autowired
	private SystemProblemService systemProblemService;
	
	@Autowired
	private AgencyService agencyService;
	
	/**
	 * Description:经销商反馈新问题
	 * @param req
	 * @param resp
	 * @param systemProblemEntity
	 * @author dingjiang
	 * @date 2017年8月23日上午11:34:29
	 */
	@RequestMapping(value="/insert",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void insert(HttpServletRequest req, HttpServletResponse resp, SystemProblemEntity systemProblemEntity){
		try {
			//验证参数非空
			if (null == systemProblemEntity.getTitle() || StringUtils.isEmpty(systemProblemEntity.getTitle())) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "标题不能为空");
				return ;
			}
			if (null == systemProblemEntity.getContent() || StringUtils.isEmpty(systemProblemEntity.getContent())) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "内容不能为空");
				return ;
			}
			//判重
			systemProblemEntity.setFacId(getCurrentAgencyUser().getFacId());
			systemProblemEntity.setAgeId(getCurrentAgencyUser().getAgeId());
			if ( systemProblemService.selectBycondition(systemProblemEntity) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "问题记录已存在");
				return ;
			}
			systemProblemEntity.setFeederName(agencyService.find(getCurrentAgencyUser().getAgeId()).getAgeName());
			systemProblemEntity.setPhone(getCurrentAgencyUser().getPhone());
			systemProblemEntity.setCreator(getCurrentAgencyUser().getId());
			systemProblemEntity.setUpdater(getCurrentAgencyUser().getId());
			systemProblemService.insertSelective(systemProblemEntity);
			doResult(resp, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	

	/**
	 * Description:经销商查询自己发起的所有问题反馈，提供日期范围、问题名称查询
	 * @param req
	 * @param resp
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param beginTime
	 * @param endTime
	 * @param title
	 * @author dingjiang
	 * @date 2017年9月4日下午5:51:47
	 */
	@RequestMapping(value="/selectpage",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectPage(HttpServletRequest req, HttpServletResponse resp, Integer page, Integer rows, 
			String sort, String order, String beginTime, String endTime, Byte type, Byte status ){
		try {
			if (null == page) {
				page = 1;
			}
			if (null == rows) {
				rows = 10;
			}
			PageUtils<SystemProblemEntity> pageinfo = new PageUtils<SystemProblemEntity>(page, rows, sort, order);
			pageinfo.setSort("status");
			pageinfo.setOrder("desc");
			pageinfo.getCondition().put("ageId = ", getCurrentAgencyUser().getAgeId());
			pageinfo.getCondition().put("facId = ", getCurrentAgencyUser().getFacId());
			if ( !StringUtils.isEmpty(beginTime) && !StringUtils.isEmpty(endTime) ) {
				pageinfo.getCondition().put("createTime between "+beginTime+" and ", endTime );
			}
			if ( type != null ) {
				pageinfo.getCondition().put("type = ", type );
			}
			if ( status != null ) {
				pageinfo.getCondition().put("status = ", status );
			}
			systemProblemService.selectpage(pageinfo);
			doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}
	}

	
	/**
	 * Description:经销商用户补充问题
	 * @param req
	 * @param resp
	 * @param id
	 * @param text
	 * @author dingjiang
	 * @date 2017年9月4日上午10:30:22
	 */
	@RequestMapping(value="/replenish",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void replenish(HttpServletRequest req, HttpServletResponse resp, Long id, String text){
		try {
			//验证反馈ID非空
			if (LongUtils.isEmpty(id)) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请选择问题");
				return ;
			}
			SystemProblemEntity systemProblemEntity = systemProblemService.find(id, getCurrentAgencyUser().getAgeId());
			//验证反馈id是否合法
			if ( null == systemProblemEntity ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_ERROR);
				return ;
			}
			//判断问题状态
			if ( systemProblemEntity.getStatus() == ProblemStatus.CLOSED ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "问题已关闭");
				return ;
			}
			//验证补充内容非空
			if (StringUtils.isEmpty(text)) {
				doResult(resp, false, StatusCode.SAVEERROR, null, "请输入补充内容");
				return ;
			}
			//补充问题
			ProblemInteractiveEntity interactive = new ProblemInteractiveEntity();
			interactive.setType((byte) 2);
			interactive.setContent(text);
			interactive.setProId(id);
			interactive.setCreator(getCurrentAgencyUser().getId());
			interactive.setUpdater(getCurrentAgencyUser().getId());
			systemProblemService.reply(interactive);
			//修改问题状态
			systemProblemEntity.setStatus(ProblemStatus.PENDING);
			systemProblemEntity.setUpdater(getCurrentAgencyUser().getId());
			systemProblemService.updateByPrimaryKeySelective(systemProblemEntity);
			doResult(resp, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	
	
	/**
	 * Description:经销商用户查看问题详情
	 * @param req
	 * @param resp
	 * @param id
	 * @author dingjiang
	 * @date 2017年8月23日下午3:34:21
	 */
	@RequestMapping(value="/viewDetails",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void viewDetails(HttpServletRequest req, HttpServletResponse resp,  @Param("id")Long id ){
		try {
			//验证非空
			if (LongUtils.isEmpty(id)) {
				doResult(resp, false, StatusCode.FINDERROR, null, ResultType.INPUT_NULL);
				return ;
			}
			//判断输入id是否合法
			SystemProblemEntity systemProblemEntity = systemProblemService.find(id,getCurrentAgencyUser().getAgeId());
			if ( null == systemProblemEntity ) {
				doResult(resp, false, StatusCode.FINDERROR, null, ResultType.INPUT_ERROR);
				return ;
			}
			//判断问题是否已关闭
			if ( systemProblemEntity.getStatus() == ProblemStatus.CLOSED ) {
				doResult(resp, false, StatusCode.FINDERROR, null, "问题已关闭");
				return ;
			}
			doResult(resp, true, StatusCode.FINDSUCCESS, systemProblemEntity, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}
	}

	
	/**
	 * Description:经销商用户修改问题
	 * @param req
	 * @param resp
	 * @param systemProblemEntity
	 * @author dingjiang
	 * @date 2017年8月23日下午3:49:07
	 */
	@RequestMapping(value="/modify",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void modify(HttpServletRequest req, HttpServletResponse resp, SystemProblemEntity systemProblemEntity){
		try {
			//验证id非空
			if ( LongUtils.isEmpty(systemProblemEntity.getId()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_NULL);
				return ;
			}
			//判断id正确
			if ( null == systemProblemService.find( systemProblemEntity.getId(), getCurrentAgencyUser().getAgeId()) ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_ERROR);
				return ;
			}
			//只对title进行判重
			if ( !StringUtils.isEmpty(systemProblemEntity.getTitle()) ) {
				systemProblemEntity.setAgeId(getCurrentAgencyUser().getAgeId());
				if ( systemProblemService.selectBycondition(systemProblemEntity) ) {
					doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_ERROR);
					return ;
				}
			}
			systemProblemEntity.setUpdater(getCurrentAgencyUser().getId());
			systemProblemService.updateByPrimaryKeySelective(systemProblemEntity);
			doResult(resp, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	

	/**
	 * Description:经销商用户关闭问题(只能关闭自己发起的问题)
	 * @param req
	 * @param resp
	 * @param id
	 * @author dingjiang
	 * @date 2017年8月23日下午1:53:18
	 */
	@RequestMapping(value="/close",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void close(HttpServletRequest req, HttpServletResponse resp, @Param("id")Long id){
		try {
			//验证非空
			if (LongUtils.isEmpty(id)) {
				doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_NULL);
				return ;
			}
			//判断输入id是否合法
			SystemProblemEntity systemProblemEntity = systemProblemService.find(id, getCurrentAgencyUser().getAgeId());
			if ( null == systemProblemEntity || getCurrentAgencyUser().getId() != systemProblemEntity.getCreator() ) {
				doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_ERROR);
				return ;
			}
			systemProblemEntity.setStatus(ProblemStatus.CLOSED);
			systemProblemEntity.setUpdater(getCurrentAgencyUser().getId());
			systemProblemService.updateByPrimaryKeySelective(systemProblemEntity);
			doResult(resp, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.SAVEERROR, null, e.getMessage());
		}
	}
	

}
